{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Solution Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Problem: Implement selection sort.\n",
    "\n",
    "* [Constraints](#Constraints)\n",
    "* [Test Cases](#Test-Cases)\n",
    "* [Algorithm](#Algorithm)\n",
    "* [Code](#Code)\n",
    "* [Unit Test](#Unit-Test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Constraints\n",
    "\n",
    "* Is a naive solution sufficient (ie not stable, not based on a heap)?\n",
    "    * Yes\n",
    "* Are duplicates allowed?\n",
    "    * Yes\n",
    "* Can we assume the input is valid?\n",
    "    * No\n",
    "* Can we assume this fits memory?\n",
    "    * Yes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Test Cases\n",
    "\n",
    "* None -> Exception\n",
    "* [] -> []\n",
    "* One element -> [element]\n",
    "* Two or more elements"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Algorithm\n",
    "\n",
    "Wikipedia's animation:\n",
    "![alt text](http://upload.wikimedia.org/wikipedia/commons/9/94/Selection-Sort-Animation.gif)\n",
    "\n",
    "We can do this recursively or iteratively.  Iteratively will be more efficient as it doesn't require the extra space overhead with the recursive calls.\n",
    "\n",
    "* For each element\n",
    "    * Check every element to the right to find the min\n",
    "    * If min < current element, swap\n",
    "\n",
    "Complexity:\n",
    "* Time: O(n^2) average, worst, best\n",
    "* Space: O(1) iterative, O(m) recursive where m is the recursion depth (unless tail-call elimination is available, then O(1))\n",
    "    * Note: Tail call elimination is not inherently available in Python, see the following [StackOverflow post](http://stackoverflow.com/a/13592002).\n",
    "\n",
    "Misc: \n",
    "\n",
    "* In-place\n",
    "* Most implementations are not stable, due to swapping of values\n",
    "\n",
    "Selection sort might be a good option if moving elements is more expensive than comparing them, as it requires at most n-1 swaps.\n",
    "\n",
    "The finding of a minimum element can be done with a min heap, which would change the worst-case run time to O(n log(n)) and increase the space to O(n).  This is called a heap sort."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class SelectionSort(object):\n",
    "\n",
    "    def sort(self, data):\n",
    "        if data is None:\n",
    "            raise TypeError('data cannot be None')\n",
    "        if len(data) < 2:\n",
    "            return data\n",
    "        for i in range(len(data) - 1):\n",
    "            min_index = i\n",
    "            for j in range(i + 1, len(data)):\n",
    "                if data[j] < data[min_index]:\n",
    "                    min_index = j\n",
    "            if data[min_index] < data[i]:\n",
    "                data[i], data[min_index] = data[min_index], data[i]\n",
    "        return data\n",
    "\n",
    "    def sort_iterative_alt(self, data):\n",
    "        if data is None:\n",
    "            raise TypeError('data cannot be None')\n",
    "        if len(data) < 2:\n",
    "            return data\n",
    "        for i in range(len(data) - 1):\n",
    "            self._swap(data, i, self._find_min_index(data, i))\n",
    "        return data\n",
    "\n",
    "    def sort_recursive(self, data):\n",
    "        if data is None:\n",
    "            raise TypeError('data cannot be None')\n",
    "        if len(data) < 2:\n",
    "            return data\n",
    "        return self._sort_recursive(data, start=0)\n",
    "\n",
    "    def _sort_recursive(self, data, start):\n",
    "        if data is None:\n",
    "            return\n",
    "        if start < len(data) - 1:\n",
    "            swap(data, start, self._find_min_index(data, start))\n",
    "            self._sort_recursive(data, start + 1)\n",
    "        return data\n",
    "\n",
    "    def _find_min_index(self, data, start):\n",
    "        min_index = start\n",
    "        for i in range(start + 1, len(data)):\n",
    "            if data[i] < data[min_index]:\n",
    "                min_index = i\n",
    "        return min_index\n",
    "\n",
    "    def _swap(self, data, i, j):\n",
    "        if i != j:\n",
    "            data[i], data[j] = data[j], data[i]\n",
    "        return data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Unit Test\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting test_selection_sort.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile test_selection_sort.py\n",
    "import unittest\n",
    "\n",
    "\n",
    "class TestSelectionSort(unittest.TestCase):\n",
    "\n",
    "    def test_selection_sort(self, func):\n",
    "        print('None input')\n",
    "        self.assertRaises(TypeError, func, None)\n",
    "\n",
    "        print('Empty input')\n",
    "        self.assertEqual(func([]), [])\n",
    "\n",
    "        print('One element')\n",
    "        self.assertEqual(func([5]), [5])\n",
    "\n",
    "        print('Two or more elements')\n",
    "        data = [5, 1, 7, 2, 6, -3, 5, 7, -10]\n",
    "        self.assertEqual(func(data), sorted(data))\n",
    "\n",
    "        print('Success: test_selection_sort\\n')\n",
    "\n",
    "\n",
    "def main():\n",
    "    test = TestSelectionSort()\n",
    "    selection_sort = SelectionSort()\n",
    "    test.test_selection_sort(selection_sort.sort)\n",
    "    try:\n",
    "        test.test_selection_sort(selection_sort.sort_recursive)\n",
    "        test.test_selection_sort(selection_sort.sor_iterative_alt)\n",
    "    except NameError:\n",
    "        # Alternate solutions are only defined\n",
    "        # in the solutions file\n",
    "        pass\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None input\n",
      "Empty input\n",
      "One element\n",
      "Two or more elements\n",
      "Success: test_selection_sort\n",
      "\n",
      "None input\n",
      "Empty input\n",
      "One element\n",
      "Two or more elements\n"
     ]
    }
   ],
   "source": [
    "%run -i test_selection_sort.py"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
